/*jshint esversion: 9 */
/*
 小工具
 */

export function removeSpace(data) {  //字符串去除空格
    if (data) {
        data = data.replace(/\s+/g, ""); //去空格
    }
    return data;
}
export function flattenTwoList(list = []) { //将二维数组展开
    let dataList = [];
    for (let item of list) {
        dataList.push(...item);
    }
    return dataList;
}
export function highlightFilter(text,hlText){  //高亮文本过滤器
    if(!text || !hlText) return text;
    return text.replace(new RegExp(hlText,'g'),`<span>${hlText}</span>`);
}
export function delHtmlTag(str){  //字符串去掉所有的html标记
    if(!str) return str;
    return str.replace(/<[^>]+>/g,"");
}
export function createObjectURL(file) {  //创建文件路径
    var url = null;
    if (window.createObjectURL !== undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL !== undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL !== undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}
// export function test(list){  //判断数组里数字正负是否统一
//     let a = 0;  //和
//     let b = 0;  //绝对值和
//     list.forEach(item=>{
//         if(typeof item != 'number' || Number.isNaN(item)) return;
//         a += item;
//         b += Math.abs(item);
//     });
//     return Math.abs(a) == Math.abs(b);
// }
export function isProdoction(){  //是否是生产环境
    return process.env.NODE_ENV === "production";
}
/** 
 * 设置element表单中lebel的最大宽度
 */
export function setupFormLabelWidth(refEl){
    if(!refEl) return;
    let widthList = [];
    const labelElList = refEl.querySelectorAll('label');
    labelElList.forEach(item=>{
        item.style.width = 'fit-content';
        widthList.push(item.clientWidth + 1);
    });
    labelElList.forEach(item=>{
        item.style.width = `${Math.max(...widthList)}px`;
    });
}
/** 
 * 将数字格式化为千分位字符
 */
export function toThousandsString(num) {
    return String(num).replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,');
}
/** 
 * 展开树形list结构为一维list 
 * @param treeList:array
 * @param option:object
 */
export function unfoldTreeList(treeList,option={}){
    const { 
        childsKey='children',  //需要获取子元素的 key
        setParentKey='parentId',  //需要赋值的父元素 key 
        getParentKey='id', //获取父元素 key
        forEachFn=null,  //循环时的回调
    } = option;
    const deptOneList = [];
    const deepForEach = (target)=>{
        deptOneList.push(target);
        forEachFn && forEachFn(target);
        if(target[childsKey]){
            target[childsKey].forEach(item_=>{
                item_[setParentKey] = target[getParentKey];
                deepForEach(item_);
            });
        }
    }
    treeList.forEach(item=>{
        deepForEach(item);
    });
    return deptOneList;
}